#include "RadianRenderWnd.h"

#include <QtGui>
#include <QtGui/QWidget>
#include "OgreRoot.h"
#include "OgreSceneNode.h"
#include "OgreCamera.h"
#include "OgreViewport.h"
#include "OgreRenderWindow.h"
#include "OgreSceneManager.h"
#include "core/logManager.h"
#include "OgreLight.h"

//--------------------------------------------------------------------------
RadianRenderWnd::RadianRenderWnd(const Ogre::String& wndName,QWidget* parentWidget,bool isPrimaryWnd)
	:mRenderWndName(wndName)
	,mParentWidget(parentWidget)
	,mSceneMgr(NULL)
	,mCamera(NULL)
	,mViewport(NULL)
	,mRenderWindow(NULL)
	,mBackgroundColour(G_EditorDefaultBackgroundColour)
	,mIsPrimaryRenderWnd(isPrimaryWnd)
	,mCameraNode(NULL)
{

}
//--------------------------------------------------------------------------
RadianRenderWnd::~RadianRenderWnd(void)
{

}
//--------------------------------------------------------------------------
bool RadianRenderWnd::Initialize()
{
	assert(mParentWidget);

	CreateRenderWindow();
	
	CreateSceneMgr();

	CreateCamera();

	CreateViewPort();

	return true;
}
//--------------------------------------------------------------------------
void RadianRenderWnd::CreateRenderWindow()
{
	WId wndId = mParentWidget->winId();
	Ogre::NameValuePairList params; 
	params["left"]                 = "0";  
    params["top"]                  = "0";  
    params["border"]               = "none";  
	params["externalWindowHandle"] = Ogre::StringConverter::toString((size_t)(HWND)wndId);  

	mRenderWindow = Ogre::Root::getSingletonPtr()->createRenderWindow(mRenderWndName
																		,mParentWidget->width()
																		,mParentWidget->height()
																		,false
																		,&params);

	mRenderWindow->setDeactivateOnFocusChange(false);

}
//--------------------------------------------------------------------------
void RadianRenderWnd::CreateViewPort()
{
	assert(mCamera);
	
	mViewport = mRenderWindow->addViewport(mCamera);
	mViewport->setAutoUpdated(true);
	mViewport->setBackgroundColour(mBackgroundColour);

}
//--------------------------------------------------------------------------
void RadianRenderWnd::CreateSceneMgr()
{
	if(mIsPrimaryRenderWnd)
	{
		Ogre::LogManager::getSingletonPtr()->logMessage("*** Create SceneManager ***");
		mSceneMgr = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, G_EditorMainSceneMgrName);

	}

	else
	{
		Ogre::LogManager::getSingletonPtr()->logMessage("*** Create Sub SceneManager:" + mRenderWndName + "_SceneMgr ***");
		mSceneMgr = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, mRenderWndName + "_SceneMgr");
	}

	mSceneMgr->setAmbientLight(Util::ColourValue(0.25f, 0.25f, 0.25f));
	mMainLight = mSceneMgr->createLight(mRenderWndName + "_MainLight");
	mMainLight->setType(Ogre::Light::LT_DIRECTIONAL);
	mMainLight->setDirection(0, -1.0f, -0.5f);

	Ogre::SceneNode* lightSceneNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
	lightSceneNode->attachObject(mMainLight);
}
//--------------------------------------------------------------------------
void RadianRenderWnd::CreateCamera()
{
	mCamera = mSceneMgr->createCamera(mIsPrimaryRenderWnd? G_EditorMainCameraName : mRenderWndName + "_Camera");

	mCamera->setAutoAspectRatio(true);
	mCamera->setNearClipDistance(1);
	mCamera->setFarClipDistance(100000.0f);
	mCamera->setProjectionType(Ogre::PT_PERSPECTIVE);
	mCamera->setPolygonMode(Ogre::PM_SOLID);
	mCamera->setPosition(0,500,500);
	mCamera->lookAt(0,0,0);
}
//--------------------------------------------------------------------------
void RadianRenderWnd::SceneSetup()
{
	

}
//--------------------------------------------------------------------------
void RadianRenderWnd::Update(float timeElapsed, bool realTime)
{
	if(!mRenderWindow)
		return;

	// update the renderwindow.
	if(!realTime)
		mRenderWindow->update();
}
//--------------------------------------------------------------------------
Ogre::SceneManager* RadianRenderWnd::GetSceneMgr()
{
	return mSceneMgr;
}
//--------------------------------------------------------------------------
Ogre::Camera* RadianRenderWnd::GetCamera()
{
	return mCamera;
}
//--------------------------------------------------------------------------
Ogre::Viewport* RadianRenderWnd::GetViewport()
{
	return mViewport;
}
//--------------------------------------------------------------------------
Ogre::RenderWindow* RadianRenderWnd::GetRenderWindow()
{
	return mRenderWindow;
}
//--------------------------------------------------------------------------
Util::ColourValue RadianRenderWnd::GetBackgroundColour()
{
	return mBackgroundColour;
}
//--------------------------------------------------------------------------
void RadianRenderWnd::WindowResize(unsigned int width,unsigned int height)
{
	if(mRenderWindow)
		mRenderWindow->resize(width,height);
}
//--------------------------------------------------------------------------
